﻿using ReportMealManagementSystem.MyControl.ViewModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ReportMealManagementSystem
{
    /// <summary>
    /// UserControlMenuItem.xaml 的交互逻辑
    /// </summary>
    public partial class UserControlMenuItem : UserControl
    {
        StackPanel stackPanel;
        ItemMenu menu;
        MainWindow _context;
        public UserControlMenuItem(ItemMenu itemMenu, MainWindow context)
        {
            InitializeComponent();

            _context = context;

            ExpanderMenu.Visibility = itemMenu.SubItems == null ? Visibility.Collapsed : Visibility.Visible;
            ListViewItemMenu.Visibility = itemMenu.SubItems == null ? Visibility.Visible : Visibility.Collapsed;

            menu = itemMenu;
            this.DataContext = itemMenu;
            foreach (SubItem subItem in itemMenu.SubItems)
            {
                if (MenuGlobal.stackPanel.Count == 0 && subItem.StackPanel != null)
                {
                    MenuGlobal.stackPanel.Add(subItem.StackPanel);
                }
                else
                {
                    for(int index = 0; index < MenuGlobal.stackPanel.Count; index++)
                    {
                        
                        if (subItem.StackPanel != null)
                        {
                            var sp = MenuGlobal.stackPanel.Where(t => t.Name == subItem.StackPanel.Name).FirstOrDefault();
                            if (sp == null)
                            {
                                MenuGlobal.stackPanel.Add(subItem.StackPanel);
                            }
                        }
                    }
                }
            }
        }

        private void ListViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _context.SwitchScreen(((SubItem)((ListView)sender).SelectedItem).Screen);
        }

        private void ListViewItemMenu_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            _context.SwitchScreen(((SubItem)((ListView)sender).SelectedItem).Screen);
            for (int index = 0; index < MenuGlobal.stackPanel.Count; index++)
            {
                MenuGlobal.stackPanel[index].Visibility = Visibility.Collapsed;
            }
        }

        private void ListViewMenu_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _context.SwitchScreen(((SubItem)((ListView)sender)?.SelectedItem)?.Screen);
            if (ListViewMenu.SelectedValue != null)
            {
                stackPanel = menu.SubItems[ListViewMenu.SelectedIndex].StackPanel;
                for (int index = 0; index < MenuGlobal.stackPanel.Count; index++)
                {
                    MenuGlobal.stackPanel[index].Visibility = Visibility.Collapsed;
                }
                if (stackPanel != null)
                {
                    stackPanel.Visibility = Visibility.Visible;
                }
            }
        }
    }
}
